// pages/managePoster/managePoster.js
const app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    project_detail: {},
    posterFileInfo: [],
    posterInfo: [],
  },
  // ----------------------------------------------------------------------

  navBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  navToDealPerson(e) {
    wx.showLoading({
      mask: true,
      title: '加载中',
    })
    let index = e.currentTarget.dataset.index;
    let obj = {
      project_detail: this.data.project_detail,
      thePosterInfo: this.data.posterInfo[index],
      thePosterFileInfo: this.data.posterFileInfo[index]
    };
    app.navTmpInfo = obj;
    wx.navigateTo({
      url: '/pages/projectDealPerson/projectDealPerson',
    })
    setTimeout(() => {
      wx.hideLoading();
    }, 3000);

  },

  //递归查询用户信息
  findPoster(arr, i, posterInfo) {
    let that = this;
    console.log(arr);
    if (arr.length == 0) {
      this.setData({
        isShow: false
      })
      return;
    }
    // console.log(arr[i].sender_ID);
    db.collection('users').where({
      _openid: arr[i].sender_ID
    }).get({
      success: res => {
        if (res.data.length != 0) {
          posterInfo.push(res.data[0]);
        }
        if (arr.length != i + 1) {
          return that.findPoster(arr, i + 1, posterInfo);
        } else {
          that.setData({
            posterInfo: posterInfo
          });
          console.log(that.data.posterInfo);
        }
      },
      fail: res => {
        console.log(res);
      }
    })
  },


  // ----------------------------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      project_detail: app.navTmpInfo.project_detail,
      posterFileInfo: app.navTmpInfo.posterFileInfo
    });


    let posterInfo = [];
    this.findPoster(app.navTmpInfo.posterFileInfo, 0, posterInfo);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})